#include "DanceLayer.h"

USING_NS_CC;

bool DanceLayer::init()
{
	auto winSize = Director::getInstance()->getWinSize();
	greenBallSp = Sprite::create("r1.png");
	greenBallSp->setPosition(Vec2(winSize.width/2,winSize.height/2));
	this->addChild(greenBallSp,1000,1000);

	greenBallSonSp = Sprite::create("r1.png");
	greenBallSonSp->setPosition(Vec2(winSize.width/4,0));
	greenBallSp->addChild(greenBallSonSp,1,GREEN_BALL_SON_TAG);

	auto streak = MotionStreak::create(2, 3, 32, Color3B::GREEN, s_streak);
	addChild(streak,3,STREAK_TAG);

	auto colorAction = RepeatForever::create(Sequence::create(
		TintTo::create(0.2f, 255, 0, 0),
		TintTo::create(0.3f, 0, 255, 0),
		TintTo::create(0.1f, 0, 0, 255),
		TintTo::create(0.4f, 0, 255, 255),
		TintTo::create(0.1f, 255, 255, 0),
		TintTo::create(0.3f, 255, 69, 0),
		TintTo::create(0.5f, 255, 255, 255),
		NULL));

	streak->runAction(colorAction);

	auto r = RotateBy::create(6,360);
	auto action = RepeatForever::create(r);
	greenBallSp->runAction(action);	
	
	auto a1 = MoveBy::create(5,Vec2(200,0));

	auto action1 = RepeatForever::create(Sequence::create(a1->clone(),a1->reverse(),NULL));
	greenBallSp->runAction(action1);

	schedule(schedule_selector(DanceLayer::onUpdate));
	return true;
}

void DanceLayer::onUpdate(float dt)
{
	auto streak =  this->getChildByTag(STREAK_TAG);
	//auto greenBallSon = (Sprite*)this->getChildByTag(GREEN_BALL_SON_TAG);
	streak->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
	streak->setPosition(greenBallSonSp->convertToWorldSpaceAR(Vec2::ANCHOR_MIDDLE));
}